<template>
  <div class='wrapper'>
    <img class="wrapper_img" src="http://www.dell-lee.com/imgs/vue3/user.png" alt="">
    <div class="wrapper_input">
      <input class="wrapper_input_content" placeholder="请输入手机号" type="text"  v-model="mobile">
    </div>
    <div class="wrapper_input">
      <input
        class="wrapper_input_content"
        placeholder="请输入密码"
        type="password"
        v-model="password"
        autocomplete="new-password">
    </div>
    <div class="wrapper_input">
      <input
        class="wrapper_input_content"
        placeholder="确认密码"
        type="password"
        v-model="confirmPwd"
        autocomplete="new-password">
    </div>
    <div class="wrapper_register-button" @click="handleRegisterClick">注  册</div>
    <div class="wrapper_register-link" @click="handleLoginClick">已有账号去登录</div>
    <Toast v-if="show" :message="toastMessage"></Toast>
  </div>
</template>

<script>
import { useRouter } from 'vue-router'
import { reactive, toRefs } from 'vue'
import { post } from '@/utils/request'
import Toast, { useToastEffect } from '@/components/Toast'

// 注册逻辑
const useRegisterEffect = (showToast) => {
  const router = useRouter()
  const data = reactive({
    mobile: '',
    password: '',
    confirmPwd: ''
  })
  const handleRegisterClick = async () => {
    try {
      const result = await post('/api/user/register', {
        username: data.mobile,
        password: data.password
      })
      if (result.errno === 0) {
        showToast('注册成功')
        router.push({ name: 'Login' })
      } else {
        showToast('注册失败')
      }
    } catch (error) {
      showToast('请求失败')
    }
  }
  const { mobile, password, confirmPwd } = toRefs(data)
  return { handleRegisterClick, mobile, password, confirmPwd }
}
// 已有账号跳转登录页
const useLoginEffect = () => {
  const router = useRouter()
  const handleLoginClick = () => {
    router.push({ name: 'Login' })
  }
  return { handleLoginClick }
}
export default {
  name: 'Register',
  components: { Toast },
  setup () {
    const { show, toastMessage, showToast } = useToastEffect()
    const { handleRegisterClick, mobile, password, confirmPwd } = useRegisterEffect(showToast)
    const { handleLoginClick } = useLoginEffect()
    return { handleLoginClick, handleRegisterClick, mobile, password, confirmPwd, show, toastMessage }
  },
  computed: {},
  watch: {},
  methods: {

  },
  beforeCreate () {},
  created () {},
  beforeMount () {},
  mounted () {},
  beforeUpdate () {},
  updated () {},
  beforeUnmount () {},
  Unmount () {}
}
</script>
<style lang='scss' scoped>
@import '@/style/viriables.scss';
.wrapper{
  position: absolute;
  top: 50%;
  left: 0;
  right: 0;
  transform: translateY(-50%);
  &_img{
    display: block;
    margin: 0 auto .4rem auto;
    width: .66rem;
    height: .66rem;
  }
  &_input{
    // box-sizing: border-box;
    height: .48rem;
    margin: 0  .4rem .16rem .4rem;
    padding: 0 .16rem;
    background: #f9f9f9;
    border: .01rem solid rgba(0,0,0,0.10);
    border-radius: .06rem;
    &_content{
      width: 100%;
      border: none;
      outline: none;
      background: none;
      line-height: .22rem;
      margin-top: .12rem;
      font-size: .16rem;
      color: $content-notice-fontcolor;
      &::placeholder{
        color: $content-notice-fontcolor;
      }
    }
  }
  &_register-button{
    margin: .32rem .4rem .16rem .4rem;
    line-height: .48rem;
    background: $btn-bgColor;
    box-shadow: 0 .04rem .08rem 0 rgba(0,145,255,0.32);
    border-radius: .04rem;
    color:$bg-color;
    font-size: .16rem;
    text-align: center;
  }
  &_register-link{
    text-align: center;
    font-size: .14rem;
    color: $content-notice-fontcolor;
  }
}
</style>
